<?

//require_once(dirname(__FILE__) . '/array.php');

class bf_date {

	protected $time;
	protected $opts;

	public function __construct($options = array()) {
		if (!is_array($options)) {
			$options = array('date' => $options);
		}
		$this->opts = array_merge(array(
			'date' => date('Y-m-d H:i:s'),
		), $options);
		if (($date = date_parse($this->opts['date'])) === false) {
			$this->time = false;
		} else {
			$this->time = mktime(
				intval($date['hour']),
				intval($date['minute']),
				intval($date['second']),
				intval($date['month']),
				intval($date['day']),
				intval($date['year'])
			);
		}
	}

	public function time() {
		return $this->time;
	}

	public function get($format = 'Y-m-d H:i:s') {
		if ($this->time === false)
			return '';
		return date($format, $this->time());
	}

	public function range_to($last_date, $step = 86400) {
		return $this->range($this->time(), $last_date->time(), $step);
	}

	public function range_from($first_date, $step = 86400) {
		return $this->range($first_date->time(), $this->time(), $step);
	}

	public function last_day() {
		return date('t', $this->time);
	}

	protected function range($begin, $end, $step) {
		$range = range($begin, $end, $step);
		$res = bf_array::from_array($range);
		return $res->each(create_function('$p', 'list ($value, $key, $array, $param) = $p; $array->set($key, new bf_date(date("Y-m-d", $value)));'));
	}
}

?>